﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace Buccaneer
{
    public class WorkSpace : Panel
    {
        private Form mdiForm; 
        private MdiClient ctlClient = new MdiClient();
        public WorkSpace() : base()
        {
            base.Controls.Add(this.ctlClient);

            this.AddPlugin("try1");
        }
        
        public Form MdiForm
        {
            get
            {
                if (this.mdiForm == null)
                {
                    this.mdiForm = new Form();
                    /// set the hidden ctlClient field which is used to determine if the form is an MDI form
                    FieldInfo field = typeof(Form).GetField("ctlClient", BindingFlags.NonPublic | BindingFlags.Instance); field.SetValue(this.mdiForm, this.ctlClient);
                } 
                return this.mdiForm;
            }
        }

        #region Public Functions

        public void AddPlugin(string name)
        {
            Form frm = new Form();
            frm.Name = name;
            frm.MdiParent = this.MdiForm;
            frm.Parent = this.Parent;
            frm.Show();
        }

        public void RemovePlugin(string name)
        {
            foreach (Form form in this.mdiForm.MdiChildren)
            {
                if (form.Name == name)
                {
                    form.Close();
                    break;
                }
            }
        }

        #endregion
    }
}
